//
//  CXExten.swift
//  CXExtens
//
//  Created by 陈警卫 on 2023-12-28.
//

import Foundation
import CommonCrypto

extension String {
    
    func md5() -> String {
        if let data = self.data(using: .utf8) {
            var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
            _ = data.withUnsafeBytes { (body: UnsafeRawBufferPointer) in
                CC_MD5(body.baseAddress, CC_LONG(data.count), &digest)
            }
            return digest.map { String(format: "%02hhx", $0) }.joined()
        } else {
            return ""
        }
    }
}











/**
 代码备份 百度翻译
 let q = code
 let appid = "20231227001922561"
 let salt = "\(Int(Date().timeIntervalSince1970))"
 let from = "en"
 let to = "zh"
 let secret = "IMfFp7CwkaRNolmLCtZw"
 let sign = (appid + q + salt + secret).md5()
 
 let param: [String : Any] = ["q": q, "appid": appid, "salt": salt, "from": from, "to": to, "sign": sign]
 
 let dataRequest = AF.request("https://fanyi-api.baidu.com/api/trans/vip/translate",
                              method: .get,
                              parameters: param)
 
 dataRequest.responseString { response in
     
     var invocation = invocation
     
     switch response.result {
     case .success:
         if let data = response.data {
             let json = JSON(data)
             let dst = json["trans_result"][0]["dst"].stringValue
             
             let line = end
             invocation.buffer.lines.insert(dst, at: line)
         }
         break
     case .failure:
         break
     }
 }
 */
